﻿using UnityEngine;
using System.Collections;

public class ControlManager : MonoBehaviour {

	[SerializeField]
	private PlayerAnimationController mAnimationController;

    [SerializeField]
    private PlayerScript mPlayer;

    private Vector2 mCurrentPosition;
    private float mSpeed = 5;
	// Use this for initialization
	void Start () {
        mCurrentPosition = this.transform.position;
	}
	
	// Update is called once per frame
	void Update () {

		if(Input.GetKeyDown(KeyCode.LeftArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.WEST);
		}
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            mCurrentPosition.x -= mSpeed * Time.deltaTime;
            this.transform.position = mCurrentPosition;
        }
		if(Input.GetKeyUp(KeyCode.LeftArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_STAY);
		}

		if(Input.GetKeyDown(KeyCode.RightArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.EAST);
		}
        if (Input.GetKey(KeyCode.RightArrow))
        {
            mCurrentPosition.x += mSpeed * Time.deltaTime;
            this.transform.position = mCurrentPosition;
        }
		
		if(Input.GetKeyUp(KeyCode.RightArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_STAY);
		}

		if(Input.GetKeyDown(KeyCode.DownArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.SOUTH);
		}
        if (Input.GetKey(KeyCode.DownArrow))
        {
            mCurrentPosition.y -= mSpeed * Time.deltaTime;
            this.transform.position = mCurrentPosition;
        }
		if(Input.GetKeyUp(KeyCode.DownArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_STAY);
		}

		if(Input.GetKeyDown(KeyCode.UpArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.NORTH);
		}
        if (Input.GetKey(KeyCode.UpArrow))
        {
            mCurrentPosition.y += mSpeed * Time.deltaTime;
            this.transform.position = mCurrentPosition;
        }
		
		if(Input.GetKeyUp(KeyCode.UpArrow)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_STAY);
		}

		if(Input.GetKeyDown(KeyCode.Space)) {
			mAnimationController.PlayAnimation(AnimationConst.ANIMATION_ATTACK);
            mPlayer.Attack();
		}
	}
}
